package cn.org.wifit.userserver.web.service.impl;

import java.io.Serializable;
import java.util.concurrent.TimeoutException;

import cn.org.wifit.core.entity.RelationalAbstractEntity;
import cn.org.wifit.userserver.web.service.iface.IMemcachedClientService;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

/**
 * 基于memcached分布式缓存的操作服务接口。<br>
 * 该接口要工作有三个前提：<br>
 * 1、在业务工程中的classes目录存在memcachedclient.properties配置文件，并且所有配置像都配置正确。<br>
 * 2、在业务工程中spring的配置引入了memcachedclientContext.xml配置文件，并且memcachedClientBuilder被正确配置<br>
 * 3、当前操作的AbstractEntity抽象方法getUniqueKey已被实现，并且返回值不为null<br>
 * 
 * 在这样的情况下，业务系统可以使用以下方式进行引用：<br>
 * @ Override
 * private XMemcachedClientService memcachedClientService;
 * @ Qualifier("memcachedClient")
 * 
 * @author wenjie
 */
public class XMemcachedClientService<V extends Object> implements IMemcachedClientService {
	
	private MemcachedClient memcachedClient;
	
	/* (non-Javadoc)
	 * @see com.helome.springcore.service.IHelomeService#insert(com.helome.springcore.entity.AbstractEntity)
	 */
	public void insert(String key , V values) {
		if(key == null || key.equals("")
			|| values == null || values.equals(""))
		{
			return;
		}
		
		if(this.memcachedClient == null)
		{
			System.out.println("未发现memcachedClient引用，请检查配置文件！");
			return;
		}
		
		try {
			// 新写或者覆盖原有数据。
			this.memcachedClient.set(key, XMemcachedClientService.EXP, values , XMemcachedClientService.TIMEOUT);
		} catch (TimeoutException e) {
			e.printStackTrace(System.out);
		} catch (InterruptedException e) {
			e.printStackTrace(System.out);
		} catch (MemcachedException e) {
			e.printStackTrace(System.out);
		}
	}

	/* (non-Javadoc)
	 * @see com.helome.springcore.service.IHelomeService#getEntity(java.io.Serializable)
	 */
	public RelationalAbstractEntity getEntity(Serializable id) {
		String unkey = id.toString();
		if(this.memcachedClient == null)
		{
			System.out.println("未发现memcachedClient引用，请检查配置文件！");
			return null;
		}
		
		RelationalAbstractEntity entity = null;
		try {
			entity = this.memcachedClient.get(unkey, XMemcachedClientService.TIMEOUT);
		} catch (TimeoutException e) {
			e.printStackTrace(System.out);
		} catch (InterruptedException e) {
			e.printStackTrace(System.out);
		} catch (MemcachedException e) {
			e.printStackTrace(System.out);
		}
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.helome.springcore.service.IHelomeService#delete(java.io.Serializable)
	 */
	public void delete(Serializable id) {
		String unkey = id.toString();
		if(this.memcachedClient == null)
		{
			System.out.println("未发现memcachedClient引用，请检查配置文件！");
			return;
		}
		
		try {
			this.memcachedClient.delete(unkey, XMemcachedClientService.TIMEOUT);
		} catch (TimeoutException e) {
			e.printStackTrace(System.out);
		} catch (InterruptedException e) {
			e.printStackTrace(System.out);
		} catch (MemcachedException e) {
			e.printStackTrace(System.out);
		}
	}

	/**
	 * 获取内置的xmemcache client服务
	 * @return
	 */
	public MemcachedClient getMemcachedClient() {
		return this.memcachedClient;
	}

	public void setMemcachedClient(MemcachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}
}